package com.eeepay.eeepay_v2.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eeepay.eeepay_v2.R;


/**
 * Created by zw on 2016/3/2 0002.
 */
public class CustomChooseView extends LinearLayout{

    private Context mContext;
    private String leftTitle;
    private TextView tv_leftTitle;
    private ImageView iv;
    private boolean isSelected = false;
    private boolean canChoose = true;
    private String typeId;

    public CustomChooseView(Context context) {
        this(context, null);
    }

    public CustomChooseView(Context context, AttributeSet attrs){
        super(context, attrs);
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.custom_choose_view, this, true);
        tv_leftTitle = (TextView) view.findViewById(R.id.tv_left_title);
        iv = (ImageView) view.findViewById(R.id.iv_right);
        //add by zhuangzeqin 2017年4月25日18:10:39 设置监听事件
        setOnClickListener(onClickListener);
    }

    public CustomChooseView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    public void setTitle(String title){
        leftTitle = title;
        tv_leftTitle.setText(title);
    }

    public String getLeftTitle(){
        return leftTitle;
    }

    public boolean getIsSelected(){
        return isSelected;
    }

    public void setIsSelected(boolean isSelected){
        if(canChoose){
            this.isSelected = isSelected;
            if(isSelected){
                iv.setVisibility(VISIBLE);
            }else{
                iv.setVisibility(GONE);
            }
        }

    }

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        if(canChoose){
//            if(MotionEvent.ACTION_DOWN == event.getAction()){
//                if(isSelected){
//                    iv.setVisibility(GONE);
//                }else{
//                    iv.setVisibility(VISIBLE);
//                }
//                isSelected = !isSelected;
//            }
//        }
//        return super.onTouchEvent(event);
//    }
    /**
     * 设置监听事件；
     */
    private View.OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(canChoose){
                if(isSelected){
                    iv.setVisibility(GONE);
                }else{
                    iv.setVisibility(VISIBLE);
                }
                isSelected = !isSelected;
            }
        }
    };



    /**
     * 设置是否可点击（选择）
     * @param canChoose
     */
    public void setCanChoose(boolean canChoose){
        this.canChoose = canChoose;
    }

    public String getTypeId() {
        return typeId;
    }

    public void setTypeId(String typeId) {
        this.typeId = typeId;
    }
}
